<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"
        name="viewport">
  <title>浩然金融系统管理平台</title>
  <#include "/common/treelib.html">
</head>
<body>
<div class="panel" style="width:100%;height:490px;float:left;">
  <ul class="ztree" id="securityTree"></ul>
</div>
</body>
<script type="text/javascript">
    $(function () {
        var demoTreeId = $("#securityTree");
        var treeName = "securityTree";
        var newCount = 1;
        var api = frameElement.api;
        var W = api.opener;
        var TreeManager = {
            /**
             *初始化方法
             */
            init: function () {
                api.button({
                    id: 'ok',
                    name: '保存',
                    callback: TreeManager.saveSecurityAssign
                });
                api.button({
                    id: 'cancel',
                    name: '关闭'
                });
                /**
                 * 设置zTree树
                 */
                var setting = {
                    async: {
                        enable: false,
                        url: TreeManager.getUrlByNodeId,
                        dataFilter: TreeManager.filter
                    },
                    check: {
                        enable: true
                    },
                    data: {
                        simpleData: {
                            enable: true
                        }
                    },
                    view: {
                        selectedMulti: true
                    },
                    edit: {
                        enable: false
                    },
                    callback: {
                        beforeExpand: TreeManager.beforeExpand,
                        onAsyncSuccess: TreeManager.onAsyncSuccess,
                        onAsyncError: TreeManager.onAsyncError,
                        onClick: TreeManager.zTreeOnClick
                    }
                };
                var nodesJson = "${nodesJson!}";

                var zNodes = eval(nodesJson);
                $.fn.zTree.init(demoTreeId, setting, zNodes);//初始化树
            },
            saveSecurityAssign: function () {
                var treeObj = $.fn.zTree.getZTreeObj("securityTree");
                var nodes = treeObj.getCheckedNodes(true);
                if (nodes == null || nodes.length == 0) {
                    Jmts.warnTips("请选择要分配的资源");
                    return false;
                }
                var ids = new StringBuilder("");
                for (var i = 0; i < nodes.length; i++) {
                    if (!nodes[i].chkDisabled) {
                        if (i == 0) {
                            ids.append(nodes[i].id + "");
                        } else {
                            ids.append("," + nodes[i].id);
                        }
                    }
                }
                if (ids == null || ids == "") {
                    Jmts.warnTips("请选择要分配的资源");
                    return false;
                }
                Jmts.confirm("确定要分配权限吗？", function () {
                    var url = "/security/security_assign";
                    var data = {
                        ids: ids.toString(),
                        type: '${type!}',
                        assignId: '${assignId!}'
                    }
                    Jmts.ajaxPost(url, data, function (result) {
                        if (result.success) {
                            W.Jmts.succTips(result.msg);
                            window.location.reload();
                        } else {
                            W.Jmts.warnTips(result.msg);
                        }
                    });
                });
                return false;
            },
            /**
             * 展开之前执行的函数
             * @param treeId
             * @param treeNode
             * @return
             */
            beforeExpand: function (treeId, treeNode) {
                if (!treeNode.isAjaxing) {
                    TreeManager.ajaxGetNodes(treeNode, "refresh");
                    return true;
                } else {
                    Jmts.alert("Loading...");
                    return false;
                }
            },
            /**
             * 加载成功后执行的函数
             * @param event 封装了js的事件
             * @param treeId 树控件的Id
             * @param treeNode 树节点对象
             * @param msg 返回的JSON格式的消息
             * @return
             */
            onAsyncSuccess: function (event, treeId, treeNode, msg) {
                if (!msg || msg.length == 0) {
                    return;
                }
                var zTree = $.fn.zTree.getZTreeObj(treeName);
                treeNode.icon = "";
                zTree.updateNode(treeNode);//更新树结构
                zTree.selectNode(treeNode.children[0]);//设置为第一个子节点为选中状态
            },
            onAsyncError: function (event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
                var zTree = $.fn.zTree.getZTreeObj(treeName);
                Jmts.alert("Error ! 异步获取数据异常");
                treeNode.icon = "";
                zTree.updateNode(treeNode);
            },
            ajaxGetNodes: function (treeNode, reloadType) {
                var zTree = $.fn.zTree.getZTreeObj(treeName);
                if (reloadType == "refresh") {
                    zTree.updateNode(treeNode);
                }
                zTree.reAsyncChildNodes(treeNode, reloadType, true);
            },
            /**
             * 功能：当点击树节点时，调用该函数
             * @param event
             * @param treeId
             * @param treeNode
             * @return
             */
            zTreeOnClick: function (event, treeId, treeNode) {
            },
            filter: function (treeId, parentNode, childNodes) {
                if (!childNodes) return null;
                for (var i = 0, l = childNodes.length; i < l; i++) {
                    childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
                }
                return childNodes;
            }
        };
        TreeManager.init();
    });
</script>
</html>
